package com.nfit.shopweb.web.api;

import com.github.pagehelper.PageInfo;
import com.nfit.shopmodel.dto.system.AssginRoleDto;
import com.nfit.shopmodel.dto.system.SysPasswordDto;
import com.nfit.shopmodel.dto.system.SysUserDto;
import com.nfit.shopmodel.entity.system.SysUser;
import com.nfit.shopmodel.vo.common.Result;
import com.nfit.shopmodel.vo.common.ResultCodeEnum;
import com.nfit.shopweb.service.SysUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName:SysUserController
 * Package:com.nfit.shopweb.web.api
 * Description
 *
 * @Author L
 * @Create 2024/11/17 19:57
 */
@Tag(name = "user",description = "用户管理")
@RestController
@RequestMapping("/api/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    public Result page(@PathVariable("pageNum") Integer pageNum,
                       @PathVariable("pageSize") Integer pageSize,
                       @RequestBody(required = false) SysUserDto sysUserDto)
    {

       PageInfo<SysUser> pageInfo = sysUserService.page(pageNum,pageSize,sysUserDto);
       return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        sysUserService.delete(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PutMapping("/update")
    public Result update(@RequestBody SysUser sysUser) {
        sysUserService.update(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/save")
    public Result save(@RequestBody SysUser sysUser) {
        sysUserService.save(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/saveAssignRole")
    public Result saveAssignRole(@RequestBody AssginRoleDto assginRoleDto) {
        sysUserService.saveAssignRole(assginRoleDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改密码
     * @param sysPasswordDto 封装前端传来的数据
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody SysPasswordDto sysPasswordDto) {
        sysUserService.updatePassword(sysPasswordDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
